//
//  DSLeaderboardView.m
//  DSJY
//
//  Created by 冯华强 on 16/9/26.
//  Copyright © 2016年 dsjy. All rights reserved.
//  排行榜的View

#import "DSLeaderboardView.h"
#import "DSLearnScore.h"
#import "MTConst.h"
#import "Masonry.h"
#import "DSLearnInfo.h"
#import "UIColor+Extension.h"
#import "DSAccountViewModel.h"

#define LhMargin 12
#define RhMargin 8
@interface DSLeaderboardView ()
//图标
@property(nonatomic, weak) UIImageView *iconView;
//名次
@property (weak, nonatomic) UILabel *rankingLabel;
//姓名
@property (weak, nonatomic) UILabel *nickNameLabel;
//积分
@property (weak, nonatomic) UILabel *integralLabel;
@end

@implementation DSLeaderboardView

#pragma mark - 系统回调
+ (__kindof DSLeaderboardView *)leaderboardView
{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupView];
    }
    return self;
}

#pragma mark - 设置UI
- (void)setupView
{
    //包裹内容的view,为了有间隙
    UIView *contentView = [[UIView alloc] init];
    [self addSubview:contentView];
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.bottom.mas_equalTo(0);
    }];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    iconView.hidden = YES;
    iconView.image = [UIImage imageNamed:@"rose"];
    [self addSubview:iconView];
    self.iconView = iconView;
    
    self.rankingLabel = [self createLabelWithSuperView:contentView];
    self.nickNameLabel = [self createLabelWithSuperView:contentView];
    self.integralLabel = [self createLabelWithSuperView:contentView];

    [self.rankingLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(LhMargin);
        make.centerY.mas_equalTo(0);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rankingLabel.mas_left);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.nickNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
    }];
    
    [self.integralLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-RhMargin);
        make.centerY.mas_equalTo(0);
    }];
}

- (UILabel *)createLabelWithSuperView:(UIView *)superView
{
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = setFont16;
    titleLabel.textColor = [UIColor color333333];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [superView addSubview:titleLabel];
    return titleLabel;
}

- (void)setLearnInfo:(DSLearnInfo *)learnInfo
{
    _learnInfo = learnInfo;
    
    if (learnInfo == nil) {
        self.nickNameLabel.text = @"";
        self.rankingLabel.text = @"";
        self.integralLabel.text = @"";
    }else{
        self.nickNameLabel.text = learnInfo.studentname;
//        self.rankingLabel.text = learnInfo.paiming;
        self.integralLabel.text = learnInfo.score;
        self.rankingLabel.text = learnInfo.isEmpty ? @"" : [NSString stringWithFormat:@"%@", learnInfo.paiming];
    }
    
    self.iconView.hidden = ![learnInfo.studentid isEqualToString:[DSAccountViewModel sharedAccount].account.stuId];
}

- (void)setupTextColor:(UIColor *)textColor
{
    self.nickNameLabel.textColor = textColor;
    self.rankingLabel.textColor = textColor;
    self.integralLabel.textColor = textColor;
}

@end
